import datetime
import time
import traceback

import pymysql
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from random import choice

import proxy


def sendEmail(text, imgUrl):
    mail_host = 'smtp.163.com'
    mail_user = 'leefengme'
    mail_pass = 'ZHNYRICPUNCRAMHO'
    sender = 'leefengme@163.com'
    receivers = ['leefengme@163.com']
    message = MIMEMultipart('related')
    message.attach(MIMEText(f'<img src="cid:dns_config" alt="dns配置"> \n{text}', 'html', 'utf-8'))
    img = MIMEImage(requests.get(imgUrl).content)
    img.add_header('Content-ID', 'dns_config')
    message.attach(img)
    message['Subject'] = '关注的房源信息变动'
    message['From'] = sender
    # 接受方信息
    message['To'] = receivers[0]
    try:
        smtpObj = smtplib.SMTP()
        # 连接到服务器
        smtpObj.connect(mail_host, 25)
        # 登录到服务器
        smtpObj.login(mail_user, mail_pass)
        # 发送
        smtpObj.sendmail(
            sender, receivers, message.as_string())
        # 退出
        smtpObj.quit()
        print('success')
    except smtplib.SMTPException as e:
        print('error', e)


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'
}


def getData(xiaoquName, conn, proxy):
    url = f'https://bj.ke.com/ershoufang/{xiaoquName}/'
    result = requests.get(url, headers=headers, proxies={'http': proxy, 'https':proxy}).text
    bf = BeautifulSoup(result, features="html.parser")
    list = bf.find("div", attrs={
        'data-component': 'list'
    })
    if list is not None:
        houses = list.find_all('li', attrs={
            'class': 'clear'
        })
        xiaoqu_id = xiaoquName.split('/')[0]
        print(f'{datetime.datetime.now()} {xiaoqu_id} houses: {len(houses)} proxy: {proxy}')
        houseIds = []
        for a in houses:
            if a.attrs['class'] == ['clear']:
                cursor = conn.cursor()
                try:
                    unitPriceTag = a.find(attrs={'class': 'unitPrice'})
                    price = a.find(attrs={'class': 'priceInfo'}).find(name='span').text.strip()
                    houseInfo = a.find(attrs={'class': 'houseInfo'}).text.replace(" ", '').replace('\n', '')
                    houseId = unitPriceTag.attrs['data-hid']
                    houseIds.append(houseId)
                    unitPrice = unitPriceTag.find(name='span').text
                    img = a.find(attrs={'class': 'lj-lazy'}).attrs['data-original']
                    taxfree = ''
                    if a.find(attrs={'class': 'taxfree'}) is not None:
                        taxfree = a.find(attrs={'class': 'taxfree'}).text
                    followInfo = a.find(attrs={'class': 'followInfo'}).text.replace(' ', '').replace('\n', '').split(
                        '/')
                    focusNum = followInfo[0][:followInfo[0].index('人')]
                    xiaoquNameString = a.find(attrs={'class': 'positionInfo'}).find(name='a').text
                    needInsert = True

                    with conn.cursor() as c:
                        c.execute(
                            f"select house_id,price,catch_date from focus_house where house_id={houseId} and offline = false order by catch_date desc")
                        dbHouse = c.fetchone()
                        if dbHouse is not None and dbHouse[1] == price:
                            needInsert = False
                    if needInsert:
                        print(f'有变更 {xiaoqu_id} {houseId}，插入新数据 {houseInfo}')
                        cursor.execute(
                            f"insert into focus_house(house_id,img,price,info,unit_price,tax_free,focus_num,shelf_date,xq_id) values({houseId},'{img}',{price},'{xiaoquNameString} {houseInfo}','{unitPrice}','{taxfree}','{focusNum}','{followInfo[1]}','{xiaoqu_id}')")
                        conn.commit()
                        houseItem = a.find(attrs={'class': 'info'})
                        with conn.cursor() as c:
                            c.execute(
                                f"select price,catch_date from focus_house where house_id={houseId} order by catch_date desc")
                            dbHouses = c.fetchall()
                            _history = '<br/>'
                            for h in dbHouses:
                                _history = f"{_history}<br/>{h[0]}w {h[1]}"
                            sendEmail(f'{houseItem} \n\n 历史：{_history}', img)
                    else:
                        cursor.execute(
                            f"update focus_house set shelf_date='{followInfo[1]}' where house_id={houseId}")
                        conn.commit()
                except Exception as e:
                    print(e)
                finally:
                    cursor.close()

        with conn.cursor() as c:
            c.execute(f"select * from focus_house where xq_id='{xiaoqu_id}' and offline=false group by house_id")
            _houseArray = c.fetchall()
            for item in _houseArray:
                if not contains(houseIds, f'{item[1]}'):
                    c.execute(f"update focus_house set offline=true where house_id='{item[1]}'")
                    conn.commit()

                    c.execute(
                        f"select price,catch_date from focus_house where house_id={item[1]} order by catch_date desc")
                    dbHouses = c.fetchall()
                    _history = ''
                    for h in dbHouses:
                        _history = f"{_history}<br/>{h[0]}w {h[1]}"
                    print('房源已下架或出售')
                    sendEmail(f'房源已下架或出售\n 价格：{item[3]}\n房屋：{item[4]} \n\n 历史：{_history}', item[2])

    else:
        print(f"{xiaoquName} 网页错误，没有找到节点")
        sendEmail(f'{result}',
                  'https://images.669pic.com/element_min_new_pic/3/37/93/21/ae8cebcea12d98dadc46f4479f3da211.png')


def contains(array, item):
    for i in array:
        if i == item:
            return True
    return False


if __name__ == '__main__':
    proxies = proxy.getProxy()
    # proxies = ['39.175.89.211:80']
    print(f"proxies:{proxies}")
    if len(proxies) > 0:
        conn = pymysql.connect(host='82.156.52.41', port=23306, user='root', passwd='zR@Dew4!FdcUE:k', db='fang')
        try:
            getData('c1111027381311', conn, choice(proxies))
            getData('c1111040862993/bp0ep430', conn, choice(proxies))
            getData('ie2c1111027379219', conn, choice(proxies))
            getData('sf1c1111027377917', conn, choice(proxies))
            getData('c1111027379007', conn, choice(proxies))
            getData('lc1lc2lc4p3p4c1111040862993', conn, choice(proxies))
            getData('bp250ep350c1111027375191', conn, choice(proxies))
            getData('f5p4sq280', conn, choice(proxies))
            getData('sf1l2bp300ep520c1111027379085', conn, choice(proxies))  # 上奥世纪 2室 300-520w
            getData('sf1l2bp300ep500c1111027376997', conn, choice(proxies))  # 金榜园 2室 300-500w
            getData('sf1lc1lc2l2bp300ep500c1111027378122', conn, choice(proxies))  # 龙乡小区东区 2室 300-500w 中低楼层
            getData('sf1lc1lc2bp0ep460c1111027378235', conn, choice(proxies))  # 龙泽苑西区 不超过460万中低楼层
            getData('l2l3bp0ep460c1111027381003', conn, choice(proxies))  # 新龙城 不超过460 2，,3室
            getData('co21bp0ep420c1111027374438', conn, choice(proxies))  # 风雅园三区 不超过420
            getData('co21lc1lc2lc4bp0ep430c1111027381983', conn, choice(proxies))  # 云趣园三区 不超过430万 中低楼层
            getData('dp1sf1lc1lc2lc4l2l3bp0ep400c1111027380050', conn, choice(proxies))  # 天通苑东三区 不超过400 中低楼层 普通住在 商品房
            getData('dp1sf1lc1lc2lc4f5l2l3bp0ep400c1111027380051', conn, choice(proxies))  # 天通苑东一区 不超400万 中低楼层 普通住宅 南北向
            getData('dp1sf1f5l2l3bp0ep400c1111027380049', conn, choice(proxies))  # 天通苑东二区 不超400万 中低楼层 普通住宅 南北向
            getData('dp1bp0ep400c1111027380057', conn, choice(proxies))  # 天通苑中区 不超过400万 商品房
            getData('dp1sf1lc1lc2f5l2l3bp0ep350rs回龙观', conn, choice(proxies))  # 回龙观不超过250 南北 中低楼层 普通住宅 商品房 两室三室
            getData('dp1sf1lc1lc2lc3f2f5bp0ep400sq363', conn, choice(proxies))  # 路劲世界城不超过400 中低高楼层 普通住宅 商品房 南北 南
            getData('ba0ea120c1220053596026828', conn, choice(proxies))  # 天津农垦含章雅筑 不超过120平
        except Exception as e:
            sendEmail(f'程序报错：<br/> {traceback.format_exc()}',
                      'https://images.669pic.com/element_min_new_pic/3/37/93/21/ae8cebcea12d98dadc46f4479f3da211.png')
        finally:
            print("finally")
            conn.close()
    else:
        print("代理地址获取失败")
        sendEmail(f'程序报错：<br/> 没有获取到代理',
                  'https://images.669pic.com/element_min_new_pic/3/37/93/21/ae8cebcea12d98dadc46f4479f3da211.png')
